package com.cmeduSystem.webadmin.upms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.cmeduSystem.common.core.base.dao.BaseDaoMapper;
import com.cmeduSystem.common.core.base.service.BaseService;
import com.cmeduSystem.common.core.object.TokenData;
import com.cmeduSystem.webadmin.upms.dao.SysConfMapper;
import com.cmeduSystem.webadmin.upms.model.SysConf;
import com.cmeduSystem.webadmin.upms.service.SysConfService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Slf4j
@Service("sysConfService")
public class SysConfServiceImpl extends BaseService<SysConf, Long> implements SysConfService {

    @Autowired
    private SysConfMapper sysConfMapper;

    /**
     * 返回当前Service的主表Mapper对象。
     *
     * @return 主表Mapper对象。
     */
    @Override
    protected BaseDaoMapper<SysConf> mapper() {
        return sysConfMapper;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean update(SysConf sysConf, SysConf originalSysConf) {
        sysConf.setId(originalSysConf.getId());
        sysConf.setUpdateUserId(TokenData.takeFromRequest().getUserId());
        sysConf.setUpdateTime(new Date());
        UpdateWrapper<SysConf> uw = this.createUpdateQueryForNullValue(sysConf, sysConf.getId());
        return sysConfMapper.update(sysConf, uw) == 1;
    }

    @Override
    public SysConf getSysConfByCode(String confCode) {
        return sysConfMapper.selectOne(new LambdaQueryWrapper<SysConf>().eq(SysConf::getConfCode, confCode));
    }
}